package gpeerreview.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.user.client.Window;

class HttpClient implements RequestCallback
{
	private Request m_request;
	public String m_results;
	public String m_error;

	public void onError(Request request, java.lang.Throwable exception)
	{
		Window.alert("Got an exception: " + exception);
	}

	public void onResponseReceived(Request request, Response response)
	{
		if(response.getStatusCode() != Response.SC_OK)
			m_error = response.getStatusText();
		else
			m_results = response.getText();
	}

	public void getUrl(String url) throws RequestException
	{
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
		//rb.setTimeoutMillis(20000);
		rb.setCallback(this);
		m_request = rb.send();
	}

	public boolean isPending()
	{
		return m_request.isPending();
	}

	public void cancel()
	{
		m_request.cancel();
		m_request = null;
	}
}
